from django import http
from django.core.paginator import Paginator

from django.shortcuts import render
from django.views import View

from apps.contents.utils import get_categories
from apps.goods.models import GoodsCategory, SKU
from apps.goods.utils import get_breadcrumb

from utils.response_code import RETCODE

from apps.goods.models import GoodsVisitCount
"""
搜索

1.我们要运行搜索引擎
2.搜索引擎可以帮助我们对数据进行检索
3.我们需要借助于HackStack来实现和elasticsearch的交互



把数据给搜索引擎,搜索引擎会建立搜索的对应关系

我 是 中 国 人   --> 搜索引擎 --> 分词 --> 我,中国,中国人,国人,是中,人
I have a pen        I       Hava        a       pen
i have an apple

"""
class ListView(View):
    """
    # 1.分类数据
    # ２．面包屑
    # ３．列表数据
    """
    def get(self,request,category_id,page_num):
        # 1.分类数据
        categories=get_categories()


        # ２．面包屑
        try:
            category = GoodsCategory.objects.get(pk=category_id)
        except GoodsCategory.DoesNotExist:

            return http.HttpResponseBadRequest('GoodsCategory does not exist')


        # 查询商品频道分类
        # categories = get_categories()
        # 查询面包屑导航
        breadcrumb = get_breadcrumb(category)
        context = {
            'categories': categories,
            'breadcrumb': breadcrumb,

        }

        # 按照排序规则查询该分类商品SKU信息
        # 接收sort参数：如果用户不传，就是默认的排序规则
        sort = request.GET.get('sort', 'default')
        if sort == 'price':
            # 按照价格由低到高
            sort_field = 'price'
        elif sort == 'hot':
            # 按照销量由高到低
            sort_field = '-sales'
        else:
            # 'price'和'sales'以外的所有排序方式都归为'default'
            sort = 'default'
            sort_field = 'create_time'
        skus = SKU.objects.filter(category=category).order_by(sort_field)

        # 创建分页器：每页N条记录
        paginator = Paginator(skus,5)
        # 获取每页商品数据
        try:
            page_num=int(page_num)
            page_skus = paginator.page(page_num)
        except Exception as e:
            pass
        # 获取列表页总页数
        total_page = paginator.num_pages
        context = {
            'categories': categories,
            'breadcrumb': breadcrumb,

            'sort': sort,  # 排序字段
            'category': category,  # 第三级分类
            'page_skus': page_skus,  # 分页后数据
            'total_page': total_page,  # 总页数
            'page_num': page_num,  # 当前页码
        }
        # ３．列表数据

        return render(request,'list.html',context=context)


class HotGoodsView(View):
    """商品热销排行"""

    def get(self, request, category_id):
        """提供商品热销排行JSON数据"""
        # 根据销量倒序
        skus = SKU.objects.filter(category_id=category_id).order_by('-sales')[:2]

        # 序列化
        hot_skus = []
        for sku in skus:
            hot_skus.append({
                'id':sku.id,
                'default_image_url':sku.default_image.url,
                'name':sku.name,
                'price':sku.price,
            })

        return http.JsonResponse({'code':RETCODE.OK, 'errmsg':'OK', 'hot_skus':hot_skus})


class DetailView(View):
    """商品详情页"""

    def get(self, request, sku_id):
        """提供商品详情页"""
        # 获取当前sku的信息
        try:
            sku = SKU.objects.get(id=sku_id)
        except SKU.DoesNotExist:
            return render(request, '404.html')

        # 查询商品频道分类
        categories = get_categories()
        # 查询面包屑导航
        breadcrumb = get_breadcrumb(sku.category)

        # 构建当前商品的规格键
        sku_specs = sku.specs.order_by('spec_id')
        sku_key = []
        for spec in sku_specs:
            sku_key.append(spec.option.id)
        # 获取当前商品的所有SKU
        skus = sku.spu.sku_set.all()
        # 构建不同规格参数（选项）的sku字典
        spec_sku_map = {}
        for s in skus:
            # 获取sku的规格参数
            s_specs = s.specs.order_by('spec_id')
            # 用于形成规格参数-sku字典的键
            key = []
            for spec in s_specs:
                key.append(spec.option.id)
            # 向规格参数-sku字典添加记录
            spec_sku_map[tuple(key)] = s.id
        # 获取当前商品的规格信息
        goods_specs = sku.spu.specs.order_by('id')
        # 若当前sku的规格信息不完整，则不再继续
        if len(sku_key) < len(goods_specs):
            return
        for index, spec in enumerate(goods_specs):
            # 复制当前sku的规格键
            key = sku_key[:]
            # 该规格的选项
            spec_options = spec.options.all()
            for option in spec_options:
                # 在规格参数sku字典中查找符合当前规格的sku
                key[index] = option.id
                option.sku_id = spec_sku_map.get(tuple(key))
            spec.spec_options = spec_options

        # 渲染页面
        context = {
            'categories': categories,
            'breadcrumb': breadcrumb,
            'sku': sku,
            'specs': goods_specs,
        }
        return render(request, 'detail.html', context)

class DetailVisitView(View):
    """详情页分类商品访问量"""

    def post(self, request, category_id):
        # 1.接收分类id
        # 2.查询对应的分类信息
        try:
            category = GoodsCategory.objects.get(pk=category_id)
        except GoodsCategory.DoesNotExist:
            return http.JsonResponse({'code': RETCODE.NODATAERR, 'errmsg': '没有此分类'})
        # 3.统计数据的更新
        # 3.1 先查询对应的当天和对应分类的记录
        # category
        # today 的日期
        from django.utils import timezone
        today = timezone.localdate()

        try:
            # gvc=GoodsVisitCount.objects.filter(category=category,date=today)[0]
            gvc = GoodsVisitCount.objects.get(category=category, date=today)
        except GoodsVisitCount.DoesNotExist:
            # 查询不出来
            # 3.2 创建新的记录
            gvc = GoodsVisitCount(
                category=category,
                count=1,
                date=today
            )

            gvc.save()

        else:
            # 查询出来了
            # 3.3 更新数据
            gvc.count += 1
            gvc.save()

        # 4.返回数据
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'ok'})





